package test.blued.com.casecontroller;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import test.blued.com.casecontroller.commenthod.CommandMethod;

/**
 * Created by mm on 2017-11-17.
 */

public class AutoCases extends Activity {

    public CommandMethod menthod = new CommandMethod();
    public String CMDStr00 = "adb shell am instrument --user 0 -w -r -e class " +
            "test.testCases.liveCases.TestCaseLive#test01Live_LiveList, ";
     public String CMDStr01 = "-e debug false com.blued.international.test/test.communal.InstrumentationTestRunner";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto_cases);
        Intent intent = getIntent();
        String software = intent.getStringExtra("software");
        findView();
        init();
        if(software.equals("international")){
            nearly_game.setClickable(false);
            nearly_game.setBackgroundColor(Color.parseColor("#CDCDCD"));
            dynamic_pointList.setClickable(false);
            dynamic_pointList.setBackgroundColor(Color.parseColor("#CDCDCD"));
            dynamic_pointDetail.setClickable(false);
            dynamic_pointDetail.setBackgroundColor(Color.parseColor("#CDCDCD"));
            end = international;
        }

        login.setOnCheckedChangeListener(new myCheckedChangeListener());
        nearly.setOnCheckedChangeListener(new myCheckedChangeListener());
        group.setOnCheckedChangeListener(new myCheckedChangeListener());
        news.setOnCheckedChangeListener(new myCheckedChangeListener());
        dynamic.setOnCheckedChangeListener(new myCheckedChangeListener());
        live.setOnCheckedChangeListener(new myCheckedChangeListener());
        person.setOnCheckedChangeListener(new myCheckedChangeListener());

        begin.setOnClickListener(new View.OnClickListener() {          //执行自动化cases
            @Override
            public void onClick(View v) {
                Toast.makeText(AutoCases.this, getCMDStr(), Toast.LENGTH_LONG).show();
                Log.i("T11", head + getCMDStr() + end);
                menthod.theCMD(head + getCMDStr() + end);
            }
        });

        report.setOnClickListener(new View.OnClickListener() {         //跳转到查看日志页面
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(AutoCases.this, HandleFile.class);
                startActivity(intent);
            }
        });

    }

    private class myCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView.getId() == R.id.login);
            switch(buttonView.getId()){
                case R.id.login:
                    if(isChecked){
                        for(int i=0; i<loginList.size(); i++){
                            loginList.get(i).setChecked(true);
                            if(!loginList.get(i).isClickable()){
                                loginList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<loginList.size(); i++){
                            loginList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.nearly:
                    if(nearly.isChecked()){
                        for(int i=0; i<nearlyList.size(); i++){
                            nearlyList.get(i).setChecked(true);
                            if(!nearlyList.get(i).isClickable()){
                                nearlyList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<nearlyList.size(); i++){
                            nearlyList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.group:
                    if(group.isChecked()){
                        for(int i=0; i<groupList.size(); i++){
                            groupList.get(i).setChecked(true);
                            if(!groupList.get(i).isClickable()){
                                groupList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<groupList.size(); i++){
                            groupList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.news:
                    if(news.isChecked()){
                        for(int i=0; i<newsList.size(); i++){
                            newsList.get(i).setChecked(true);
                            if(!newsList.get(i).isClickable()){
                                newsList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<newsList.size(); i++){
                            newsList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.dynamic:
                    if(dynamic.isChecked()){
                        for(int i=0; i<dynamicList.size(); i++){
                            dynamicList.get(i).setChecked(true);
                            if(!dynamicList.get(i).isClickable()){
                                dynamicList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<dynamicList.size(); i++){
                            dynamicList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.live:
                    if(live.isChecked()){
                        for(int i=0; i<liveList.size(); i++){
                            liveList.get(i).setChecked(true);
                            if(!liveList.get(i).isClickable()){
                                liveList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<liveList.size(); i++){
                            liveList.get(i).setChecked(false);
                        }
                    }
                    break;
                case R.id.person:
                    if(person.isChecked()){
                        for(int i=0; i<personList.size(); i++){
                            personList.get(i).setChecked(true);
                            if(!personList.get(i).isClickable()){
                                personList.get(i).setChecked(false);
                            }
                        }
                    }else{
                        for(int i=0; i<personList.size(); i++){
                            personList.get(i).setChecked(false);
                        }
                    }
                    break;
                default: break;
            }
        }
    }

    private String getCMDStr(){
        StringBuilder sbCases = new StringBuilder();
        List<CheckBox> all = new ArrayList<CheckBox>();
        Iterator it = loginList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = nearlyList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = groupList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = newsList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = dynamicList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = liveList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        it = personList.iterator();
        while(it.hasNext()){
            all.add((CheckBox)it.next());
        }
        String []cases = {
                "test.testMod.login.loginCases.Log,", "test.testMod.login.loginCases.Login#test00LoginOut,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,", "test.testCases.login.Register,",
                "test.testCases.login.Register,",
        };
        for(int i=0; i<all.size(); i++){
            if(all.get(i).isChecked())
                sbCases.append(cases[i]);
        }
        return sbCases.toString();
    }

    private void findView(){
        begin = (Button)findViewById(R.id.casesBegin);
        report = (Button)findViewById(R.id.report);
        login = (CheckBox)findViewById(R.id.login);
        login_register = (CheckBox)findViewById(R.id.login_register);
        login_login = (CheckBox)findViewById(R.id.login_login);

        nearly = (CheckBox)findViewById(R.id.nearly);
        nearly_list = (CheckBox)findViewById(R.id.nearly_list);
        nearly_choose = (CheckBox)findViewById(R.id.nearly_choose);
        nearly_other = (CheckBox)findViewById(R.id.nearly_other);
        nearly_game = (CheckBox)findViewById(R.id.nearly_game);

        group = (CheckBox)findViewById(R.id.group);
        group_make = (CheckBox)findViewById(R.id.group_make);
        group_details = (CheckBox)findViewById(R.id.group_details);
        group_data = (CheckBox)findViewById(R.id.group_data);
        group_chat = (CheckBox)findViewById(R.id.group_chat);
        group_other = (CheckBox)findViewById(R.id.group_other);

        news = (CheckBox)findViewById(R.id.news);
        news_search = (CheckBox)findViewById(R.id.news_search);
        news_setting = (CheckBox)findViewById(R.id.news_setting);
        news_chat = (CheckBox)findViewById(R.id.news_chat);
        news_other = (CheckBox)findViewById(R.id.news_other);

        dynamic = (CheckBox)findViewById(R.id.dynamic);
        dynamic_release = (CheckBox)findViewById(R.id.dynamic_release);
        dynamic_list = (CheckBox)findViewById(R.id.dynamic_list);
        dynamic_details = (CheckBox)findViewById(R.id.dynamic_details);
        dynamic_other = (CheckBox)findViewById(R.id.dynamic_other);
        dynamic_pointList = (CheckBox)findViewById(R.id.dynamic_pointList);
        dynamic_pointDetail = (CheckBox)findViewById(R.id.dynamic_pointDetail);

        live = (CheckBox)findViewById(R.id.live);
        live_list = (CheckBox)findViewById(R.id.live_list);
        live_startend = (CheckBox)findViewById(R.id.live_startend);
        live_room = (CheckBox)findViewById(R.id.live_room);
        live_cross = (CheckBox)findViewById(R.id.live_cross);
        live_income = (CheckBox)findViewById(R.id.live_income);
        live_other = (CheckBox)findViewById(R.id.live_other);

        person = (CheckBox)findViewById(R.id.person);
        person_me = (CheckBox)findViewById(R.id.person_me);
        person_room = (CheckBox)findViewById(R.id.person_room);
        person_amend = (CheckBox)findViewById(R.id.person_amend);
        person_set = (CheckBox)findViewById(R.id.person_set);
        person_other = (CheckBox)findViewById(R.id.person_other);

    }

    private void init(){
        loginList = new ArrayList<CheckBox>();
        nearlyList = new ArrayList<CheckBox>();
        groupList = new ArrayList<CheckBox>();
        newsList = new ArrayList<CheckBox>();
        dynamicList = new ArrayList<CheckBox>();
        liveList = new ArrayList<CheckBox>();
        personList = new ArrayList<CheckBox>();

        loginList.add(login_register);
        loginList.add(login_login);
        nearlyList.add(nearly_list);
        nearlyList.add(nearly_choose);
        nearlyList.add(nearly_other);
        nearlyList.add(nearly_game);

        groupList.add(group_make);
        groupList.add(group_details);
        groupList.add(group_data);
        groupList.add(group_chat);
        groupList.add(group_other);

        newsList.add(news_search);
        newsList.add(news_setting);
        newsList.add(news_chat);
        newsList.add(news_other);

        dynamicList.add(dynamic_release);
        dynamicList.add(dynamic_list);
        dynamicList.add(dynamic_details);
        dynamicList.add(dynamic_pointList);
        dynamicList.add(dynamic_pointDetail);
        dynamicList.add(dynamic_other);

        liveList.add(live_list);
        liveList.add(live_startend);
        liveList.add(live_room);
        liveList.add(live_cross);
        liveList.add(live_income);
        liveList.add(live_other);

        personList.add(person_me);
        personList.add(person_room);
        personList.add(person_amend);
        personList.add(person_set);
        personList.add(person_other);

    }

    private String SMDStr;
    private String casesName;
    private String head = "am instrument --user 0 -w -r -e debug false -e class ";
    private String end = " com.soft.blued.test/test.communal.InstrumentationTestRunner";
    private String international = " com.blued.international.test/test.communal.InstrumentationTestRunner";

    private Button begin;
    private Button report;

    private CheckBox login;                 //注册
    private CheckBox login_register;                 //注册
    private CheckBox login_login;                 //登录

    private CheckBox nearly;
    private CheckBox nearly_list;                 //身边列表
    private CheckBox nearly_choose;                 //筛选
    private CheckBox nearly_game;                  //游戏 （国内）
    private CheckBox nearly_other;                 //身边其他

    private CheckBox group;                 //群组
    private CheckBox group_make;                 //创建群
    private CheckBox group_details;                 //查看群信息
    private CheckBox group_data;                 //修改群资料
    private CheckBox group_chat;                 //群聊
    private CheckBox group_other;                 //群其他

    private CheckBox news;
    private CheckBox news_search;                 //搜索消息
    private CheckBox news_setting;                 //消息设置
    private CheckBox news_chat;                 //私聊
    private CheckBox news_other;                 //消息其他

    private CheckBox dynamic;
    private CheckBox dynamic_release;                 //发动态
    private CheckBox dynamic_list;                 //动态列表
    private CheckBox dynamic_details;                 //查看动态详情
    private CheckBox dynamic_pointList;               //观点列表 （国内）
    private CheckBox dynamic_pointDetail;           //观点详情 （国内）
    private CheckBox dynamic_other;                 //动态其他

    private CheckBox live;
    private CheckBox live_list;                 //直播列表
    private CheckBox live_startend;                 //开关直播
    private CheckBox live_room;                 //直播间操作
    private CheckBox live_cross;                 //横屏直播
    private CheckBox live_income;                 //直播榜单
    private CheckBox live_other;                 //直播其他

    private CheckBox person;
    private CheckBox person_me;                 //我的
    private CheckBox person_room;                 //资料墙
    private CheckBox person_amend;                 //修改资料
    private CheckBox person_set;                 //设置
    private CheckBox person_other;                 //其他

    private List<CheckBox> loginList;
    private List<CheckBox> nearlyList;
    private List<CheckBox> groupList;
    private List<CheckBox> newsList;
    private List<CheckBox> dynamicList;
    private List<CheckBox> liveList;
    private List<CheckBox> personList;

}
